//
//  ASCertificateStorage.m
//  AppleSession
//
//  Created by 吴双 on 2023/2/8.
//

#import "ASCertificateStorage.h"

@implementation ASCertificateStorage

- (NSData *)objectForKeyedSubscript:(NSString *)key {
    NSString *path = NSHomeDirectory();
    path = [path stringByAppendingPathComponent:@".xauth/certificates"];
    path = [path stringByAppendingPathComponent:key];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        NSData *data = [NSData dataWithContentsOfFile:path];
        return data;
    }
    return nil;
}

- (void)setObject:(NSData *)obj forKeyedSubscript:(NSString *)key {
    NSString *path = NSHomeDirectory();
    path = [path stringByAppendingPathComponent:@".xauth/certificates"];
    [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    
    path = [path stringByAppendingPathComponent:key];
    [obj writeToFile:path atomically:YES];
}

@end
